package com.example.mqspring.rabiitmqbasicleverone.rabbitmqRPC;

import com.example.mqspring.utils.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.util.HashMap;
import java.util.Map;

public class MainMethod {

    public static void main(String[] args) {
//        队列
        String queue = "selfqueue";
//      消息的认证标识
        String messageIdentifier = "bobo_message";
        Connection connection = null;
//        创建连接 信道
        connection = RabbitMqUtil.createConnection();
//       根据key消息的认证标识,映射对于的动作信息
        Map<String, String> activeMap = new HashMap<>();
        activeMap.put(messageIdentifier,"入库");
        RPCClient rpcClient = new RPCClient();
        Channel channel = null;

        try {
            channel = connection.createChannel();
            RPCService rpcService = new RPCService(channel);
            RPCClientConsume rpcClientConsume = new RPCClientConsume(channel,activeMap);
            String replyQueue = channel.queueDeclare().getQueue();
//           客户端发送消息
            rpcClient.requestSend(channel,queue,messageIdentifier,replyQueue);
//          服务端消费消息,最后一个参数是回调类  autoACK =false就是要开启应答机制
            String consumTag = channel.basicConsume(queue, false, rpcService);
//           客户端消费服务端的反馈消息
            String s = channel.basicConsume(replyQueue, false, rpcClientConsume);
            channel.basicCancel(consumTag);
            channel.basicCancel(s);
        } catch (Exception e) {
            System.out.println("异常");
        } finally {
            try {
                channel.close();
                connection.close();
            } catch (Exception e) {
                System.out.println("异常");
            }
        }
    }
}
